package com.excesys.exsecs.alarm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.admin.service.SysUserService;
import com.excesys.exsecs.alarm.entity.AlarmTypeConf;
import com.excesys.exsecs.alarm.mapper.AlarmTypeConfMapper;
import com.excesys.exsecs.alarm.service.AlarmTypeConfService;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.util.Arrays;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 告警类型配置表
 *
 * @author lxc
 * @date 2024-10-14 16:40:36
 */
@Service
@RequiredArgsConstructor
public class AlarmTypeConfServiceImpl extends ServiceImpl<AlarmTypeConfMapper, AlarmTypeConf> implements AlarmTypeConfService {

    private final SysUserService sysUserService;

    @Override
    public Page queryPage(Page page, AlarmTypeConf alarmTypeConf) {
        Page<AlarmTypeConf> page1 = this.page(page, Wrappers.<AlarmTypeConf>lambdaQuery().like(StrUtil.isNotBlank(alarmTypeConf.getTypeName())
                , AlarmTypeConf::getTypeName, alarmTypeConf.getTypeName())
                .eq(StrUtil.isNotBlank(alarmTypeConf.getTypeCode()), AlarmTypeConf::getTypeCode, alarmTypeConf.getTypeCode()));
        if (CollectionUtils.isNotEmpty(page1.getRecords())) {
            page1.getRecords().forEach(it ->{
                if (StringUtils.isNotEmpty(it.getSender())){
                    String[] userId = it.getSender().split(",");
                    it.setSenderList(userId);
                    List<SysUser> users = sysUserService.list(Wrappers.<SysUser>lambdaQuery().in(SysUser::getUserId, userId));
                   if (CollectionUtils.isNotEmpty(users)) {
                       it.setSenderName(users.stream().map(SysUser::getName).collect(Collectors.joining(",")));
                   }
                }
                if (StringUtils.isNotEmpty(it.getWay())){
                    String[] ways = it.getWay().split(",");
                    it.setWayList(ways);
                }
            });
        }
        return page1;
    }

    @Override
    public AlarmTypeConf getTypeById(Integer id) {
        AlarmTypeConf conf = this.getById(id);
        if (StringUtils.isNotEmpty(conf.getSender())){
            String[] userId = conf.getSender().split(",");
            conf.setSenderList(userId);
            List<SysUser> users = sysUserService.list(Wrappers.<SysUser>lambdaQuery().in(SysUser::getUserId, userId));
            if (CollectionUtils.isNotEmpty(users)) {
                conf.setSenderName(users.stream().map(SysUser::getName).collect(Collectors.joining()));
            }
        }
        return conf;
    }

	@Override
	public AlarmTypeConf getTypeByCode(String typeCode) {
		QueryWrapper<AlarmTypeConf> queryWrapper = Wrappers.query();
		queryWrapper.eq("type_code", typeCode);
		AlarmTypeConf conf = this.baseMapper.selectOne(queryWrapper);
		if(ObjectUtil.isNotEmpty(conf)){
			if (StringUtils.isNotEmpty(conf.getSender())){
				String[] userId = conf.getSender().split(",");
				conf.setSenderList(userId);
				List<SysUser> users = sysUserService.list(Wrappers.<SysUser>lambdaQuery().in(SysUser::getUserId, userId));
				if (CollectionUtils.isNotEmpty(users)) {
					conf.setSenderName(users.stream().map(SysUser::getName).collect(Collectors.joining()));
				}
			}
		}
		return conf;
	}

	@Override
    public boolean saveType(AlarmTypeConf alarmTypeConf) {
        //产生alarmTypeCode
        alarmTypeConf.setTypeCode(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        if (!Arrays.isNullOrEmpty(alarmTypeConf.getWayList())){
            alarmTypeConf.setWay(String.join(",", alarmTypeConf.getWayList()));
        }
        if (!Arrays.isNullOrEmpty(alarmTypeConf.getSenderList())){
            alarmTypeConf.setSender(String.join(",", alarmTypeConf.getSenderList()));
        }
        return this.save(alarmTypeConf);
    }

    @Override
    public boolean updateType(AlarmTypeConf alarmTypeConf) {
        if (!Arrays.isNullOrEmpty(alarmTypeConf.getWayList())){
            alarmTypeConf.setWay(String.join(",", alarmTypeConf.getWayList()));
        }
        if (!Arrays.isNullOrEmpty(alarmTypeConf.getSenderList())){
            alarmTypeConf.setSender(String.join(",", alarmTypeConf.getSenderList()));
        }
        return this.updateById(alarmTypeConf);
    }


}